package com.booth.base.utils;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

public class UploadUtils {

	/***创建访问的前缀***/
	public static String creatHeaderUrl(HttpServletRequest request)
	{
		String scheme = request.getScheme();//http
		String serverName = request.getServerName();//localhost
		int serverPort = request.getServerPort();//端口号：8080
		String contextPath = request.getContextPath();//项目放置的web 例如根目录/ 或者自己定义的目录
		String url = scheme+"://"+serverName+":"+serverPort+contextPath;
		String headerUrl = url.replace(File.separator, "/");
//		System.out.println("headerUrl=:"+headerUrl);
		return headerUrl;
		
	}
	/***创建保存的路径***/
	public static String creatSavePath(HttpServletRequest request,String str, String myPath)
	{
		String realPath = request.getRealPath("/");
		realPath = realPath.substring(0,realPath.indexOf(":")+1);
		System.out.println("realPath=:"+realPath);
		String mySavePath = null;
		if(myPath != null && !myPath.equals(""))
		{
			mySavePath = myPath + File.separator + str;
//			System.out.println("mySavePath=:"+mySavePath);
		}
		else
			mySavePath = str;
		String savePath = (realPath+'\\'+mySavePath).replace(File.separator, "/");
//		System.out.println("savePath=:"+savePath);
		return savePath;
	}
	/***创建保存的文件名***/
	public static String creatSaveFileName(String name)
	{
		String newName = null;
		int index = name.indexOf(".");
		String sub = name.substring(index+1);//后缀名
		DateFormat dft = new SimpleDateFormat("yyMMddHHmmssSSS");
		String dftDate = dft.format(new Date());
		int random = new Random().nextInt(10000);
		newName = dftDate + random + "." + sub;
		return newName;
		
	}
}
